package com.bot.adms.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bot.adms.model.bo.*;
import com.bot.adms.model.entity.ApplyContentSpScDetail;
import com.bot.adms.model.entity.ApplyContentSpScDetailPoster;
import com.bot.adms.model.entity.ApplyContentSpScDetailText;
import com.bot.adms.model.entity.ApplyContentSpScDetailVideo;
import com.mysql.cj.xdevapi.JsonParser;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//添加投放申请json数据转换方法
public class AddApplyUtils {

    public ApplyInfoBo getApplyInfoBo(JSONObject applyInfoJson) {
        ApplyInfoBo applyInfoBo = new ApplyInfoBo();
        //基本信息
        JSONObject basicInfoJson = applyInfoJson.getJSONObject("basicInfo");
        applyInfoBo.setProject(basicInfoJson.getString("projectName"));
        applyInfoBo.setLinkManName(basicInfoJson.getString("contactName"));
        applyInfoBo.setTel(basicInfoJson.getString("phone"));
        applyInfoBo.setStartTime(basicInfoJson.getDate("startTime"));
        applyInfoBo.setEndTime(basicInfoJson.getDate("endTime"));
        applyInfoBo.setCreateTime(new Date());
        applyInfoBo.setUpdateTime(new Date());
        applyInfoBo.setStatus(applyInfoJson.getBooleanValue("sub")?2:3);
        if (applyInfoJson.getString("id") != null) {
            applyInfoBo.setId(Long.parseLong(applyInfoJson.getString("id")));
        }

        //content信息
        JSONObject contentJson = applyInfoJson.getJSONObject("applyContent");
        ApplyContentBo applyContentBo = new ApplyContentBo();
        applyContentBo.setTitle(contentJson.getString("title"));
        applyContentBo.setSpScType(Integer.parseInt(contentJson.getString("spScType"))-1);
        List<ApplyContentSpScDetailBo> applyContentSpScDetailBoList = new ArrayList<>();

        //分屏类型判断
        String laystyleStr = contentJson.getString("layStyle");
        int length = laystyleStr.length();
        if (length == 0) {
            applyContentBo.setLayStyle(0);
        }else{
            String str = contentJson.getString("layStyle").substring(contentJson.getString("layStyle").length()-3);
            applyContentBo.setLayStyle(Integer.parseInt(Dic.DICT_Layout_Style.get(Integer.parseInt(str))));
        }
        System.out.println(applyInfoJson.toJSONString());
        //分屏明细数据
        JSONArray applyContentArray = applyInfoJson.getJSONArray("screens");
        //遍历数组
       for (int i = 0; i < applyContentArray.size(); i++) {
           ApplyContentSpScDetailBo applyContentSpScDetailBo = new ApplyContentSpScDetailBo();

           JSONObject screenJsonObj = applyContentArray.getJSONObject(i);
           //海报数据
           if (screenJsonObj.getString("type").equals("海报")) {
               //分屏id
               applyContentSpScDetailBo.setScId(screenJsonObj.getInteger("screenId")-1);
               //类型
               applyContentSpScDetailBo.setType(1);
               //加载海报对象数据
               ApplyContentSpScDetailPosterBo applyContentSpScDetailPosterBo = new ApplyContentSpScDetailPosterBo();
               //处理海报数据
               applyContentSpScDetailPosterBo.setPosterUrl(screenJsonObj.getString("posterFile"));
               applyContentSpScDetailPosterBo.setResolution(Long.parseLong(screenJsonObj.getString("posterResolution")));

               //将海报数据赋值给对象
               applyContentSpScDetailBo.setApplyContentSpScDetailPosterBo(applyContentSpScDetailPosterBo);
               //将分屏明细保存给内容列表
               applyContentSpScDetailBoList.add(applyContentSpScDetailBo);
           }
           //文字数据
           else if (screenJsonObj.getString("type").equals("文字")) {
               ApplyContentSpScDetailTextBo applyContentSpScDetailTextBo = new ApplyContentSpScDetailTextBo();
               //处理分屏明细数据
               applyContentSpScDetailBo.setScId(screenJsonObj.getInteger("screenId")-1);
               applyContentSpScDetailBo.setType(0);

               //处理文字数据
               applyContentSpScDetailTextBo.setImageUrl(screenJsonObj.getString("backgroundImage"));
               applyContentSpScDetailTextBo.setLocation(Integer.parseInt(screenJsonObj.getString("disLocation")));
               applyContentSpScDetailTextBo.setFontType(Integer.parseInt(screenJsonObj.getString("font")));
               applyContentSpScDetailTextBo.setFontSize(Integer.parseInt(screenJsonObj.getString("fontSize")));
               applyContentSpScDetailTextBo.setFontColor(screenJsonObj.getString("fontColor"));
               applyContentSpScDetailTextBo.setTextContent(screenJsonObj.getString("textContent"));

               //将文字广告信息添加到分屏明细下
               applyContentSpScDetailBo.setApplyContentSpScDetailTextBo(applyContentSpScDetailTextBo);

               //将分屏明细赋值给内容
               applyContentSpScDetailBoList.add(applyContentSpScDetailBo);
           }
           //视频数据
           else if (screenJsonObj.getString("type").equals("视频")) {
               ApplyContentSpScDetailVideoBo applyContentSpScDetailVideoBo = new ApplyContentSpScDetailVideoBo();

               //处理分屏明细数据
               applyContentSpScDetailBo.setScId(screenJsonObj.getInteger("screenId")-1);
               applyContentSpScDetailBo.setType(2);

               //处理视频数据
               applyContentSpScDetailVideoBo.setVideoUrl(screenJsonObj.getString("videoFile"));
               applyContentSpScDetailVideoBo.setTotalTime(screenJsonObj.getLong("totalTime"));
               applyContentSpScDetailVideoBo.setResolution(Long.parseLong(screenJsonObj.getString("videoResolution")));

               //将视频信息添加到分平明细下
               applyContentSpScDetailBo.setApplyContentSpScDetailVideoBo(applyContentSpScDetailVideoBo);

               //将分屏数据添加到内容
               applyContentSpScDetailBoList.add(applyContentSpScDetailBo);
           }

       }

       applyContentBo.setApplyContentSpScDetailBoList(applyContentSpScDetailBoList);

       applyInfoBo.setApplyContentBo(applyContentBo);

       return applyInfoBo;
    }
}
